using System;
using System.Collections.Generic;
using System.Text;
using TTDotNet.Util;
using TTDotNet.Engine.ThreeDEngine;
using TTDotNet.Engine.Stations.Depot;
using TTDotNet.Engine.Companies;
using TTDotNet.Engine.Terrain;
using System.Drawing;

namespace TTDotNet.Engine.Create.Tools
{
    public class RoadDepotTool : Tool
    {
        private RotateType rotType;
        private Coordinate downOn;

        public RoadDepotTool(Game game)
            : base(game, ToolType.RoadDepot)
        {
            
        }

        public override void MouseDown(Point mouseLocation, GridPickData data)
        {
            downOn = data.Coord;
        }

        public override void MouseUp(Point mouseLocation, GridPickData data)
        {
            if (!downOn.IsInvalid)
            {
                rotType = GetRotateType(downOn, data.Coord);

                if (data.Coord.X != -1 && rotType != RotateType.Invalid)
                {
                    int height = Game.gameWorld.Map.GetTile(downOn).Height;
                    DDDCoordinate dddCoord = new DDDCoordinate(downOn, height);

                    BuildRoadDepot(dddCoord);
                }
            }

            downOn = Coordinate.Invalid;

        }

        public void BuildRoadDepot(DDDCoordinate coord)
        {
            RoadDepot depot = new RoadDepot(game.World.GetCompany(game.PlayerNumber), "Road Depot A", new Coordinate(coord.X, coord.Z), rotType);

            //List<DDDCoordinate> list = new List<DDDCoordinate>();
            //list.Add(coord);
            //list.Add(coord);

            //Networks.RoadStrip strip = new TTDotNet.Engine.Networks.RoadStrip(list, rotType);
            //Game.gameWorld.RoadNetwork.AddRoad(strip);

            //Tile pertile = Game.gameWorld.Map.GetTile(new Coordinate(coord.X, coord.Z));
            //pertile.LandType = LandType.Road;
            //pertile.Texture = Game.texLoader.GetTexture(Game.gameWorld.RoadNetwork.GetRoadType(coord));
            //pertile.RotateType = Game.gameWorld.RoadNetwork.GetRotateType(coord);
            //Game.gameWorld.Map.Grid.UpdateTile(pertile);
        }

        public RotateType GetRotateType(Coordinate from, Coordinate to)
        {
            if(from.Equals(to)) return RotateType.Invalid;
            if (from.X != to.X && from.Z != to.Z) return RotateType.Invalid;

            if (from.X == to.X)
            {
                if (from.Z < to.Z)
                {
                    return RotateType.Zero;
                }
                else
                {
                    return RotateType.OneEighty;
                }
            }
            else
            {
                if (from.X < to.X)
                {
                    return RotateType.Ninety;
                }
                else
                {
                    return RotateType.TwoSeventy;
                }
            }
        }

        

        public void SetRotation(RotateType type)
        {
            rotType = type;
        }

    }
}
